const db = uniCloud.database();
const dbcmd = db.command;

// 生成随机数
function generateRandomNDigitNumber(n) {
	// 生成一个 0 到 10^n - 1 之间的随机数
	const number = Math.floor(Math.random() * Math.pow(10, n));
	// 将数字转换为字符串，并在前面补 0 直到长度为 n
	return number.toString().padStart(n, '0');
}

module.exports = {
	_before: function() { // 通用预处理器

	},
	/** 创建账户
	 * 
	 */
	async signIn(userInfo) {
		const {
			account,
			password
		} = userInfo;

		// 验证 account 是否存在且为 6 位
		if (!account || account.length !== 6) {
			return {
				success: false,
				message: '账号必须为6位',
			};
		}

		// 验证 password 是否存在
		if (!password) {
			return {
				success: false,
				message: '密码不能为空',
			};
		}

		const name = '用户' + generateRandomNDigitNumber(4);
		const account_ = account + generateRandomNDigitNumber(4);
		const avatar_cloudpath_default =
			'https://mp-a52cf949-e385-4e78-b2b7-81a8dd621c31.cdn.bspapp.com/cloud/userInfo/avatar_default/';
		const avatar_cloudPath =
			avatar_cloudpath_default + generateRandomNDigitNumber(1) + ".jpg";
		let res = await db.collection('userInfo').add({
			name: name,
			account: account_,
			status: '普通用户',
			password: password,
			avatar_cloudPath: avatar_cloudPath,
		});
		const userInfo_cloud = {
			id: res.id,
			name: name,
			account: account_,
			status: '普通用户',
			password: password,
			avatar_cloudPath: avatar_cloudPath
		}
		return userInfo_cloud;

	},

	/** 搜索云端账户
	 * 
	 */
	async SearchCloudAccounts(account) {
		let res = await db.collection('userInfo').field({
			'avatar_cloudPath': true
		}).where({
			account: account,
		}).get();
		return res.data[0]
	},

	/** 登录账户
	 * 
	 */
	async LogIn(userInfo_input) {
		const {
			account,
			password
		} = userInfo_input;

		console.log(account);
		let res = await db.collection('userInfo').where({
			account: account,
		}).get();

		const userInfo_cloud = res.data[0];
		if (userInfo_cloud.password == password) {
			return {
				success: true,
				message: '登录成功',
				userInfo: userInfo_cloud,
			};
		} else {
			return {
				success: false,
				message: '登录失败',
			};
		}
	},

	/** 修改昵称
	 * 
	 */
	async ModifyName(userInfo) {
		const {
			_id,
			name
		} = userInfo

		let res = await db.collection('userInfo').where({
			_id: _id,
		}).update({
			name: name,
		})

		console.log(res);
		return {
			success: 'true',
			message: '成功修改昵称'
		}
	},

	/** 修改密码
	 * 
	 */
	async ModifyPassword(userInfo) {
		const {
			_id,
			password
		} = userInfo;

		let res = await db.collection('userInfo').where({
			_id: _id,
		}).update({
			password: password,
		})

		console.log(res);
		return {
			success: 'true',
			message: '成功修改密码'
		}
	},

	/** 修改密码
	 * 
	 */
	async ModifySex(userInfo) {
		const {
			_id,
			sex
		} = userInfo;

		let res = await db.collection('userInfo').where({
			_id: _id,
		}).update({
			sex: sex,
		})

		console.log(res);
		return {
			success: 'true',
			message: '成功修改性别'
		}
	},

	/** 修改密码
	 * 
	 */

	async ModifyphoneNumber(userInfo) {
		const {
			_id,
			phoneNumber
		} = userInfo;

		let res = await db.collection('userInfo').where({
			_id: _id,
		}).update({
			phoneNumber: phoneNumber,
		})

		console.log(res);
		return {
			success: 'true',
			message: '成功修改手机号'
		}
	}

}